package com.nbjtjc.safe.service.edu;

import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduPlanTempletMapper;
import com.nbjtjc.safe.model.edu.EduPlanTemplet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EduPlanTempletService {

	@Autowired
	private EduPlanTempletMapper eduPlanTempletMapper;
	
	public Result add(EduPlanTemplet eduPlanTemplet){
		Result result = new Result();
		if(ObjectUtil.isNull(eduPlanTemplet)){
			result.setFalse("提交内容为空，操作失败");
			return result;
		}
		int res=-1;
		//无ID为新增
		if(ObjectUtil.isNull(eduPlanTemplet.getId())){
			res = eduPlanTempletMapper.insertSelective(eduPlanTemplet);
			if(res == 0){
				result.setFalse("新增失败");
				return result;
			}
		}else{//有ID为更新
			res = eduPlanTempletMapper.updateByPrimaryKeySelective(eduPlanTemplet);
			if(res == 0){
				result.setFalse("更新失败");
				return result;
			}
		}
		
		result.setData(eduPlanTemplet);
		return result;
	}
	
	public Result delete(int id){
		Result result = new Result();
		int res = eduPlanTempletMapper.deleteByPrimaryKey(id);
		if(res == 0){
			result.setFalse("删除失败");
			return result;
		}
		return result;
	}
	
	public Result find(EduPlanTemplet eduPlanTemplet){
		Result result = new Result();
		List<EduPlanTemplet> eduPlanTemplets = new ArrayList();
		if(ObjectUtil.isNull(eduPlanTemplet.getCancel())){
			eduPlanTemplet.setCancel(0);
		}
		eduPlanTemplets = eduPlanTempletMapper.select(eduPlanTemplet);
		result.setData(eduPlanTemplets);
		return result;
	}
}
